﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Utilities.DataTypes.ExtensionMethods
{
    /// <summary>
    /// Xml扩展方法(XElement类型)
    /// </summary>
    public static class XmlExtensions
    {
        /// <summary>
        /// 合并xml，以后一个为准，只加不减
        /// </summary>
        /// <param name="Xml">原xml</param>
        /// <param name="XmlNext">目标xml</param>
        /// <returns>是否成功，根目录不一样不能成功</returns>
        public static bool Merge(this XElement Xml,XElement XmlNext)
        {
            if (Xml.Name.LocalName!=XmlNext.Name.LocalName)
            {
                return false;
            }
            else
            {
                //foreach (var item in XmlNext.Attributes())
                //{
                //    if (Xml.Attributes().Where(t=>t.Name.)
                //    {
                        
                //    }
                //}
            }
            Xml.MergeSingle(XmlNext);//根目录同步
            MergeXml(Xml, XmlNext);
            return true;
        }

        private static XElement MergeXml(XElement Xml, XElement XmlNext)
        {
            //Xml.MergeSingle(XmlNext);//根目录同步
            if (XmlNext.HasElements)
            {
                foreach (var item in XmlNext.Elements())
                {
                    if (Xml.HasElements)
                    {
                        if (Xml.Elements().Where(t=>t.Name.LocalName==item.Name.LocalName).Count()==0)
                        {
                            Xml.Add(item);
                        }
                        else
                        {
                            if (item.Attributes().Where(t => t.Name.LocalName == "#setup").Count() == 0)
                            {
                                //未完
                            }
                            //Xml.Elements().Where(t => t.Name.LocalName == item.Name.LocalName).ForEach(t => t.MergeSingle(item));
                        }
                    }
                    else
                    {
                        Xml.Add(XmlNext.Elements());
                        break;
                    }
                    if (item.HasElements)
                    {
                        MergeXml(Xml.Elements().Where(t => t.Name.LocalName == item.Name.LocalName).First(), item);
                    }
                }
            }
            return Xml;
        }

        public static bool MergeSingle(this XElement Xml, XElement XmlNext)
        {
            if (Xml.Name.LocalName != XmlNext.Name.LocalName)
            {
                return false;
            }
            //属性添加
            foreach (var item in XmlNext.Attributes())
            {
                if (Xml.Attributes().Where(t=>t.Name.LocalName==item.Name.LocalName).Count()==0)
                {
                    Xml.Add(new XAttribute(item.Name.LocalName, item.Value));
                }
            }
            return true;
        }
    }
}
